/*//////////////////////////////////////////////////////////
 /// Demo of MVC implementation in javascript UI layer /////
 /// author - Alex Li //////////////////////////////////////
 //////////////////////////////////////////////////////////*/

var Observer = Class.create();
Observer.prototype = {	
   initialize: function(executer, method){
       this.executer = executer;
       this.method = method;
   },

   Notify: function() {
		return this.method.apply(this.executer,arguments);
   },

   IsEqual:function(observer) {
		return this.executer.IsEqual(observer.executer) && this.method == observer.method;
   }
}

var ObserverChain = Class.create();
ObserverChain.prototype = {
   initialize: function(event_name){
       this.event_name = name;
       this.observer_list = [];
   },
   
   AddObserver:function(observer){
      this.observer_list.push(observer);
   },

   NotifyAllObservers: function(context) {
      for(n=0;n<this.observer_list.length;n++){
         try{
             this.observer_list[n].Notify(context);
         }catch(e){
         }
      }  
   },

   StopObserving: function(observer) {
	    for(n=0;n<this.observer_list.length;n++){
         try{
             if (this.observer_list[n].IsEqual(observer)) {
				this.observer_list.splice(n,1);
			 }
         }catch(e){
         }
      } 
   }
}

var ObserverManager = Class.create();
ObserverManager.prototype = {
   initialize: function(){
      this.event_list = [];
   },
   
   AddObserver:function(event_name,observer){
      if(this.event_list.length<=0||this.event_list[event_name]==null){
        this.event_list.push(event_name);
        this.event_list[event_name] = new ObserverChain(event_name);
      }
      this.event_list[event_name].AddObserver(observer);	
   },

   NotifyAllObservers: function(event_name,context) {
       if(this.event_list[event_name]){
       	    return this.event_list[event_name].NotifyAllObservers(context);
       }
   },

   StopObserving: function(event_name, observer) {
		if(this.event_list[event_name]){
       	    this.event_list[event_name].StopObserving(observer);
       }
   }
}

var DynamicData = Class.create();
DynamicData.prototype = {
  initialize: function(){
     this.param_list = [];   
     this.id = "";    
  },
  
  Get:function(param_name){
    var result = "";
    if(this.param_list[param_name]){
      result = this.param_list[param_name];
    }
    return result;
  },

  Set:function(param_name,param_value){
     if(this.param_list.length<=0||this.param_list[param_name]==null){
        this.param_list.push(param_name);
     }
     this.param_list[param_name] = param_value;
  },
  
  Get$:function(property_name){
     var result = ""; 
     try{
        eval ('result = this.'+property_name);
     }catch(e){}
     return result;
  },

  Set$:function(property_name,property_value){
     try{
        eval ('this.'+property_name+" = "+property_value);
     }catch(e){}
  }
}

var Context = Class.create();
Context.prototype = Class.extend({
    initialize:function(){
      this.super_initialize();
    }
},DynamicData.prototype);
